﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CITWebsite.Models;
using System.IO;
using DTO.Models;

namespace CITWebsite.Controllers
{
    public class MediaController : Controller
    {
        //
        // GET: /Media/

        [Authorize]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize]
        public ActionResult MediaList(string id)
        {
            //int PageSize = Common.pagesize;
            ViewData["TypeMediaName"] = id;
            //ViewData["PageIndex"] = "1";
            //ViewData["PageSize"] = PageSize.ToString();
            return View();
        }

        [Authorize]
        public ActionResult Delete(string id)
        {
            DataRepository dr = new DataRepository();
            dr.MediaProvider.DeleteMark(id);
            return Content("");
        }

        [Authorize]
        [HttpGet]
        public ActionResult GetForm(string id, string TypeMediaName)
        {
            MEDIA obj = null;
            //add new
            if (id == "0")
            {
                ViewData["status"] = "Thêm";
                obj = new MEDIA(0, "", "", "", "", DateTime.Now, false, false, false, TypeMediaName, User.Identity.Name, DateTime.Now, User.Identity.Name);
            }
            else
            {
                ViewData["status"] = "Cập nhật";
                DataRepository dr = new DataRepository();
                obj = dr.MediaProvider.getMedia(Convert.ToInt32(id));
                obj.Content = System.Web.HttpUtility.HtmlDecode(obj.Content);
            }
            ViewData["TypeMediaName"] = TypeMediaName;
            return View(obj);
        }

        [Authorize]
        [HttpPost]
        public ActionResult GetForm(MEDIA obj)
        {
            DataRepository dr = null;
            bool titleIsEmpty = false;
            bool titleIsDuplicate = false;
            bool isNew = false;

            obj.Title = obj.Title == null ? "" : obj.Title;

            if (String.IsNullOrEmpty(obj.Title.Trim()))
            {
                titleIsEmpty = true;
            }
            else
            {
                string mediaid = Common.RemoveUnicode(obj.Title);
                dr = new DataRepository();
                MEDIA temp = dr.MediaProvider.GetMediaByMediaID(mediaid);
                obj.Intro = obj.Intro == null ? "" : obj.Intro;
                if (obj.Intro.Length > 500)
                {
                    obj.Intro = obj.Intro.Substring(0, 500);
                }
                if (temp != null)
                {
                    titleIsDuplicate = true;
                    if (obj.ID > 0)
                    {
                        if (String.Compare(temp.Title.Trim(), obj.Title.Trim(), true) == 0)
                        {
                            titleIsDuplicate = false;
                        }
                    }
                }
            }

            //insert
            if (obj.ID == 0)
            {
                isNew = true;
                dr = new DataRepository();
                obj.Content = System.Web.HttpUtility.HtmlDecode(obj.Content);
                dr.MediaProvider.Insert(obj);
                if (titleIsEmpty == true)
                {
                    obj.Title = "cit.com.vn " + obj.ID.ToString();
                    dr.MediaProvider.Update(obj);
                }
                if (titleIsDuplicate == true)
                {
                    obj.Title = obj.Title + " " + obj.ID.ToString();
                    dr.MediaProvider.Update(obj);
                }
            }
            else
            {
                dr = new DataRepository();
                if (titleIsEmpty == true)
                {
                    obj.Title = "cit.com.vn " + obj.ID.ToString();
                }
                if (titleIsDuplicate == true)
                {
                    obj.Title = obj.Title + " " + obj.ID.ToString();
                }
                obj.UserModify = User.Identity.Name;
                obj.DateModify = DateTime.Now;
                dr.MediaProvider.Update(obj);
            }

            //update

            //upload image
            HttpPostedFileBase file = Request.Files[0];
            if (file.ContentLength > 0)
            {
                string filePath = Path.Combine(HttpContext.Server.MapPath("~/MediaImage"), obj.ID.ToString() + ".jpg");
                file.SaveAs(filePath);
            }
            else
            {
                if (isNew == true)
                {
                    string urlImageDefault = Path.Combine(HttpContext.Server.MapPath("~/image"), "no_image.gif");
                    string urlDest = Path.Combine(HttpContext.Server.MapPath("~/MediaImage"), obj.ID.ToString() + ".jpg");
                    System.IO.File.Copy(urlImageDefault, urlDest);
                }
            }

            return RedirectToAction("MediaList", new { id = obj.TypeMediaName });

        }

        [Authorize]
        [HttpGet]
        public ActionResult Banner(string id)
        {
            DataRepository dr = new DataRepository();
            MEDIA[] arr = dr.MediaProvider.getMediaAll(id).ToArray();
            ViewData["TypeMediaName"] = id;
            return View(arr);
        }

        [Authorize]
        [HttpGet]
        public ActionResult Intro(string id)
        {
            DataRepository dr = new DataRepository();
            MEDIA[] arr = dr.MediaProvider.getMediaByContainsTypeMediaName(id).ToArray();
            if (arr.Length == 0)
            {
                arr = new MEDIA[4];
                MEDIA obj = new MEDIA();
                obj.Intro = "";
                obj.TypeMediaName = "introtuvan1";
                obj.IsDelete = false;
                dr.MediaProvider.Insert(obj);
                arr[0] = obj;

                obj = new MEDIA();
                obj.Intro = "";
                obj.TypeMediaName = "introtuvan2";
                obj.IsDelete = false;
                dr.MediaProvider.Insert(obj);
                arr[1] = obj;

                obj = new MEDIA();
                obj.Intro = "";
                obj.TypeMediaName = "introtuvan3";
                obj.IsDelete = false;
                dr.MediaProvider.Insert(obj);
                arr[2] = obj;

                obj = new MEDIA();
                obj.Intro = "";
                obj.TypeMediaName = "introtuvan4";
                obj.IsDelete = false;
                dr.MediaProvider.Insert(obj);
                arr[3] = obj;
            }
            ViewData["TypeMediaName"] = id;
            return View(arr);
        }

        [Authorize]
        [HttpGet]
        public ActionResult VideoGioiThieu()
        {
            DataRepository dr = new DataRepository();
            MEDIA[] arr = dr.MediaProvider.getMediaAll("videogioithieu").ToArray();
            MEDIA video;
            if (arr.Length == 0)
            {
                video = new MEDIA();
                video.TypeMediaName = "videogioithieu";
                video.UserCreate = User.Identity.Name;
                video.UserModify = User.Identity.Name;
                video.Intro = "";
                dr.MediaProvider.Insert(video);
            }
            else
            {
                video = arr[0];
            }
            return View(video);
        }

        [Authorize]
        [HttpPost]
        public ActionResult VideoGioiThieu(FormCollection collection)
        {
            DataRepository dr = new DataRepository();
            MEDIA[] arr = dr.MediaProvider.getMediaAll("videogioithieu").ToArray();
            MEDIA video;
            video = arr[0];
            video.Intro = collection["Intro"];
            dr.MediaProvider.Update(video);

            return RedirectToAction("VideoGioiThieu", new { id = "videogioithieu" });
        }

        [Authorize]
        [HttpPost]
        public ActionResult Banner()
        {
            string id = Request["ID"];
            string TypeMediaName = Request["TypeMediaName"];
            bool isDelete = Request["IsDelete"] != null ? false : true;

            DataRepository dr = new DataRepository();
            MEDIA obj = dr.MediaProvider.getMediaBanner(Convert.ToInt32(id));
            obj.Intro = Request["Intro"];
            obj.Content = Request["Content"];
            obj.IsDelete = isDelete;
            string strUrl = Path.Combine(HttpContext.Server.MapPath("~/MediaImage"), obj.ID + ".jpg");
            dr.MediaProvider.Update(obj);

            //upload image
            HttpPostedFileBase file = Request.Files[0];
            if (file.ContentLength > 0)
            {
                string filePath = Path.Combine(HttpContext.Server.MapPath("~/MediaImage"), id + ".jpg");
                file.SaveAs(filePath);
            }

            return RedirectToAction("Banner", new { id = obj.TypeMediaName });
        }

        [Authorize]
        [HttpPost]
        public ActionResult Intro()
        {
            string id = Request["ID"];
            string TypeMediaName = Request["TypeMediaName"];

            DataRepository dr = new DataRepository();
            MEDIA obj = dr.MediaProvider.getMediaIntro(Convert.ToInt32(id));
            obj.Intro = Request["Intro"];
            string strUrl = Path.Combine(HttpContext.Server.MapPath("~/MediaImage"), obj.ID + ".jpg");
            dr.MediaProvider.Update(obj);

            //upload image
            HttpPostedFileBase file = Request.Files[0];
            if (file.ContentLength > 0)
            {
                string filePath = Path.Combine(HttpContext.Server.MapPath("~/MediaImage"), id + ".jpg");
                file.SaveAs(filePath);
            }

            return RedirectToAction("Intro", new { id = "introtuvan" });
        }


        [Authorize]
        [HttpGet]
        public ActionResult List(string id, string pPageIndex, string pTitle, string pDateFrom, string pDateTo, string pIsTop)
        {
            //id : pTypeMediaName
            int PageSize = Common.pagesize;
            int PageIndex = Convert.ToInt32(pPageIndex);

            bool IsTop = pIsTop == null ? false : (pIsTop == "true" ? true : false);
            string Title = pTitle == null ? "" : pTitle;
            DateTime DateForm = pDateFrom == null ? Common.datemin : Common.ChangeDateVNToUS(pDateFrom);
            DateTime DateTo = pDateTo == null ? Common.datemax : Common.ChangeDateVNToUS(pDateTo);


            DataRepository dr = new DataRepository();

            MEDIA[] source;
            if (IsTop != true)
                source = dr.MediaProvider.getMedia(id, Title, DateForm, DateTo).ToArray();
            else
                source = dr.MediaProvider.getMedia(id, Title, DateForm, DateTo, IsTop).ToArray();

            MEDIA[] datapaging = source.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToArray();

            int len = source.Length;

            ViewData["TotalPage"] = (len % PageSize == 0 ? len / PageSize : len / PageSize + 1).ToString();
            ViewData["PageIndex"] = PageIndex.ToString();
            ViewData["PageSize"] = PageSize.ToString();
            ViewData["TypeMediaName"] = id;

            return View(datapaging);

        }

        [Authorize]
        public ActionResult Active(string id)
        {
            MEDIA obj;
            //add new
            if (id != null)
            {
                DataRepository dr = new DataRepository();
                obj = dr.MediaProvider.getMedia(Convert.ToInt32(id));
                dr.MediaProvider.UpdateTop(obj, true);
                return RedirectToAction("MediaList", new { id = obj.TypeMediaName });
            }

            return View();

        }

        [Authorize]
        public ActionResult DeActive(string id)
        {
            MEDIA obj;
            //add new
            if (id != null)
            {
                DataRepository dr = new DataRepository();
                obj = dr.MediaProvider.getMedia(Convert.ToInt32(id));
                dr.MediaProvider.UpdateTop(obj, false);
                return RedirectToAction("MediaList", new { id = obj.TypeMediaName });
            }

            return View();

        }


    }
}
